package com.huatai.manage.service.impl;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huatai.common.util.BeanUtil;
import com.huatai.common.util.StringUtil;
import com.huatai.manage.entity.TbRangeProfileEntity;
import com.huatai.manage.mapper.TbRangeProfileMapper;
import com.huatai.manage.query.PageQuery;

import com.huatai.manage.service.TbRangeProfileService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huatai.manage.utils.ComUtil;
import com.huatai.manage.vo.PageVo;
import com.huatai.manage.vo.ResultVo;
import com.huatai.manage.vo.TbRangeProfileVo;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 区域轮廓; 服务实现类
 * </p>
 *
 * @author luoxc
 * @since 2023-03-29
 */
@Service
public class TbRangeProfileServiceImpl extends ServiceImpl<TbRangeProfileMapper, TbRangeProfileEntity> implements TbRangeProfileService {

	/**
	 * 分页查询数据列表
	 *
	 * @param pageQuery
	 * @return
	 */
	@Override
	public PageVo pageList(PageQuery<TbRangeProfileEntity> pageQuery) {
		Page<TbRangeProfileEntity> page = new Page<>(pageQuery.getCurrent(), pageQuery.getSize());
		TbRangeProfileEntity data = pageQuery.getData();
		LambdaQueryWrapper<TbRangeProfileEntity> wrapper = Wrappers.<TbRangeProfileEntity>lambdaQuery();
		wrapper.like(StringUtil.isNotBlank(data.getRangeName()), TbRangeProfileEntity::getRangeName, data.getRangeName());
		List<TbRangeProfileEntity> ranges = this.page(page, wrapper).getRecords();
		List<TbRangeProfileVo> rangeVos = new ArrayList<>();
		if (ranges.isEmpty()) return null;
		ranges.forEach(range -> {
			TbRangeProfileVo profileVo = BeanUtil.copyProperties(range, TbRangeProfileVo.class);
			if (!StringUtil.isEmpty(range.getPoints()))
				profileVo.setPoints(JSONUtil.parseArray(range.getPoints()));
			if (!StringUtil.isEmpty(range.getInnerPoints()))
				profileVo.setInnerPoints(JSONUtil.parseArray(range.getInnerPoints()));
			rangeVos.add(profileVo);
		});
		PageVo pageVo = BeanUtil.copyProperties(page, PageVo.class);
		return pageVo.setRecords(rangeVos);
	}

	/**
	 * 根据区域id获取单条数据
	 *
	 * @param rangeId
	 * @return
	 */
	@Override
	public TbRangeProfileVo getByRangeId(String rangeId) {
		LambdaQueryWrapper<TbRangeProfileEntity> wrapper = Wrappers.<TbRangeProfileEntity>lambdaQuery();
		wrapper.eq(TbRangeProfileEntity::getRangeId, rangeId);
		TbRangeProfileEntity rangeProfileEntity = this.getOne(wrapper);
		TbRangeProfileVo profileVo = BeanUtil.copyProperties(rangeProfileEntity, TbRangeProfileVo.class);
		if (!StringUtil.isEmpty(rangeProfileEntity.getPoints()))
			profileVo.setPoints(JSONUtil.parseArray(rangeProfileEntity.getPoints()));
		if (!StringUtil.isEmpty(rangeProfileEntity.getInnerPoints()))
			profileVo.setInnerPoints(JSONUtil.parseArray(rangeProfileEntity.getInnerPoints()));
		return profileVo;
	}

	/**
	 * @return PageVo
	 * @Description 根据试验id分页查询区域轮廓列表
	 * @Date 2023/4/5 14:01
	 * @Param trialIdRequest
	 */
	@Override
	public PageVo pageByTrialId(PageQuery<TbRangeProfileEntity> pageQuery) {
		Page<TbRangeProfileEntity> page = new Page<>(pageQuery.getCurrent(), pageQuery.getSize());
		LambdaQueryWrapper<TbRangeProfileEntity> wrapper = Wrappers.<TbRangeProfileEntity>lambdaQuery();
		wrapper.eq(TbRangeProfileEntity::getTrialId, pageQuery.getData().getTrialId());
		List<TbRangeProfileEntity> ranges = this.page(page, wrapper).getRecords();
		List<TbRangeProfileVo> rangeVos = new ArrayList<>();
		if (ranges.isEmpty()) return null;
		ranges.forEach(range -> {
			TbRangeProfileVo profileVo = BeanUtil.copyProperties(range, TbRangeProfileVo.class);
			if (!StringUtil.isEmpty(range.getPoints()))
				profileVo.setPoints(JSONUtil.parseArray(range.getPoints()));
			if (!StringUtil.isEmpty(range.getInnerPoints()))
				profileVo.setInnerPoints(JSONUtil.parseArray(range.getInnerPoints()));
			rangeVos.add(profileVo);
		});
		PageVo pageVo = BeanUtil.copyProperties(page, PageVo.class);
		return pageVo.setRecords(rangeVos);
	}

	/**
	 * @return List<TbRangeProfileVo>
	 * @Description 根据试验id查询区域轮廓列表
	 * @Date 2023/4/5 14:01
	 * @Param trialId
	 */
	@Override
	public List<TbRangeProfileVo> listByTrialId(String trialId) {
		LambdaQueryWrapper<TbRangeProfileEntity> wrapper = Wrappers.<TbRangeProfileEntity>lambdaQuery();
		wrapper.eq(TbRangeProfileEntity::getTrialId, trialId);
		List<TbRangeProfileEntity> ranges = this.list(wrapper);
		List<TbRangeProfileVo> rangeVos = new ArrayList<>();
		if (ranges.isEmpty()) return null;
		ranges.forEach(range -> {
			TbRangeProfileVo profileVo = BeanUtil.copyProperties(range, TbRangeProfileVo.class);
			if (!StringUtil.isEmpty(range.getPoints()))
				profileVo.setPoints(JSONUtil.parseArray(range.getPoints()));
			if (!StringUtil.isEmpty(range.getInnerPoints()))
				profileVo.setInnerPoints(JSONUtil.parseArray(range.getInnerPoints()));
			rangeVos.add(profileVo);
		});
		return rangeVos;
	}


	/**
	 * 查询所有数据
	 *
	 * @return List<TbRangeProfileVo>
	 */
	@Override
	public List<TbRangeProfileVo> rangeList() {
		List<TbRangeProfileEntity> ranges = this.list();
		List<TbRangeProfileVo> rangeVos = new ArrayList<>();
		if (ranges.isEmpty()) return null;
		ranges.forEach(range -> {
			TbRangeProfileVo profileVo = BeanUtil.copyProperties(range, TbRangeProfileVo.class);
			if (!StringUtil.isEmpty(range.getPoints()))
				profileVo.setPoints(JSONUtil.parseArray(range.getPoints()));
			if (!StringUtil.isEmpty(range.getInnerPoints()))
				profileVo.setInnerPoints(JSONUtil.parseArray(range.getInnerPoints()));
			rangeVos.add(profileVo);
		});
		return rangeVos;
	}

	/**
	 * @return List<ResultVo>
	 * @Description 根据试验id查询（壹瞰思）
	 * @Date 2023/4/4 9:49
	 * @Param
	 */
	@Override
	public List<ResultVo> getByTrialId(String trialId) {
		List<ResultVo> resultVos = new ArrayList<>();
		List<TbRangeProfileEntity> list = this.list(Wrappers.<TbRangeProfileEntity>lambdaQuery().eq(TbRangeProfileEntity::getTrialId, trialId));
		if (list.isEmpty()) return null;
		list.forEach(i -> {
			resultVos.add(new ResultVo(i.getRangeId(), i.getRangeName()));
		});
		return resultVos;
	}


	/**
	 * 新增数据
	 *
	 * @param tbRangeProfileEntity
	 * @return
	 */
	@Override
	public boolean insert(TbRangeProfileEntity tbRangeProfileEntity) {
		tbRangeProfileEntity.setPoints(JSONUtil.parseArray(tbRangeProfileEntity.getPoints()).toString());
		tbRangeProfileEntity.setInnerPoints(JSONUtil.parseArray(tbRangeProfileEntity.getInnerPoints()).toString());
		return this.save(tbRangeProfileEntity.setRangeId(ComUtil.getUUID()));
	}

	/**
	 * 根据区域id集合批量删除数据
	 *
	 * @param rangeIds
	 * @return
	 */
	@Override
	public boolean removes(List<String> rangeIds) {
		if (rangeIds.isEmpty()) return false;
		LambdaQueryWrapper<TbRangeProfileEntity> wrapper = Wrappers.<TbRangeProfileEntity>lambdaQuery();
		wrapper.in(TbRangeProfileEntity::getRangeId, rangeIds);
		return this.remove(wrapper);
	}

	/**
	 * 根据区域id修改数据
	 *
	 * @param tbRangeProfileEntity
	 * @return
	 */
	@Override
	public boolean update(TbRangeProfileEntity tbRangeProfileEntity) {
		if (tbRangeProfileEntity.getRangeId().isEmpty()) return false;
		LambdaUpdateWrapper<TbRangeProfileEntity> wrapper = Wrappers.<TbRangeProfileEntity>lambdaUpdate();
		wrapper.eq(TbRangeProfileEntity::getRangeId, tbRangeProfileEntity.getRangeId());
		return this.update(tbRangeProfileEntity, wrapper);
	}
}
